//
//  LoginViewController.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2019/1/3.
//  Copyright © 2019年 郑逸凯. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginManager.h"
#import "YAUserDefault.h"

@interface LoginViewController ()
@property (nonatomic,strong)UIImageView *backView;
@property (nonatomic,strong)UIImageView *iconView;
@property (nonatomic,strong)UIImageView *accountView;
@property (nonatomic,strong)UIImageView *pswdView;
@property (nonatomic,strong)UIImageView *accountIcon;
@property (nonatomic,strong)UIImageView *pswdIcon;
@property (nonatomic,strong)UITextField *accountTextField;
@property (nonatomic,strong)UITextField *pswdTextField;
@property (nonatomic,strong)UIButton *loginButton;
@property (nonatomic,strong)UILabel *messageLabel;
@end

@implementation LoginViewController



#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubviews];
    [self performLayout];
    
    NSString *account = [YAUserDefault ya_objectForKey:KEY_ACCOUNT];
    if (![NSString tm_emptyString:account]) {
        self.accountTextField.text = account;
    }
//    self.pswdTextField.text = [YAUserDefault objectForKey:KEY_PSWD];
//    [self loginAction];
    // Do any additional setup after loading the view.
}
#pragma mark - Public Methods
#pragma mark - Responder Actions
- (void)loginAction {
    NSString *account = self.accountTextField.text;
    NSString *pswd = self.pswdTextField.text;
    if ([NSString tm_emptyString:account]) {
        [YAToast showTouchableTipMessageInView:@"请输入账号" timer:1];
        return;
    }
    if ([NSString tm_emptyString:pswd]) {
        [YAToast showTouchableTipMessageInView:@"请输入密码" timer:1];
        return;
    }
    MBProgressHUD *hud = [YAToast showActivityMessageInView:@"" timer:10];
    [[LoginManager sharedInstance] LoginWithAccount:account pswd:pswd completionHandler:^(id  _Nonnull responseObject, NSError * _Nonnull error) {
        [hud hide:YES];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (!error) {
            [self.view endEditing:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
        }else {
            [YAToast showTouchableTipMessageInView:error.domain timer:1];
        }
    }];
}

#pragma mark - Delegates
#pragma mark - Private Methods
- (void)performLayout {
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(59 + Height_StatusBar);
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.centerX.equalTo(self.backView);
    }];
    
    [self.accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).with.offset(82);
        make.size.mas_equalTo(CGSizeMake(296, 46));
        make.centerX.equalTo(self.backView);
    }];
    
    [self.accountIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.centerY.equalTo(self.accountView);
    }];
    
    [self.accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountIcon.mas_right).with.offset(13);
        make.right.equalTo(self.accountView).with.offset(-10);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self.accountView);
    }];
    
    [self.pswdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.accountView.mas_bottom).with.offset(15);
        make.size.equalTo(self.accountView);
        make.centerX.equalTo(self.backView);
    }];
    
    [self.pswdIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.centerY.equalTo(self.pswdView);
    }];
    
    [self.pswdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pswdIcon.mas_right).with.offset(13);
        make.right.equalTo(self.pswdView).with.offset(-10);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(self.pswdView);
    }];
    
    
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pswdView.mas_bottom).with.offset(35);
        make.centerX.equalTo(self.backView);
        make.size.mas_equalTo(CGSizeMake(296, 56));
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.backView).with.offset(-16);
        make.left.right.equalTo(self.backView);
        make.height.mas_equalTo(12);
    }];
}

- (void)setupSubviews {
    [self.view addSubview:self.backView];
    [self.backView addSubview:self.iconView];
    [self.backView addSubview:self.loginButton];
    [self.backView addSubview:self.accountView];
    [self.accountView addSubview:self.accountIcon];
    [self.accountView addSubview:self.accountTextField];
    [self.backView addSubview:self.pswdView];
    [self.pswdView addSubview:self.pswdIcon];
    [self.pswdView addSubview:self.pswdTextField];
    [self.backView addSubview:self.messageLabel];
    
}

#pragma mark - Setters and Getters

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"login_btn_back"] forState:UIControlStateNormal];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 10, 0);
        [_loginButton addTarget:self action:@selector(loginAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"launch_icon"]];
    }
    return _iconView;
}

- (UIImageView *)backView {
    if (!_backView) {
        _backView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_back"]];
        _backView.userInteractionEnabled = YES;
    }
    return _backView;
}

- (UIImageView *)accountView {
    if (!_accountView) {
        _accountView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_matrix"]];
        _accountView.userInteractionEnabled = YES;
    }
    return _accountView;
}

- (UIImageView *)pswdView {
    if (!_pswdView) {
        _pswdView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_matrix"]];
        _pswdView.userInteractionEnabled = YES;
    }
    return _pswdView;
}

- (UIImageView *)accountIcon {
    if (!_accountIcon) {
        _accountIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_account"]];
    }
    return _accountIcon;
}

- (UIImageView *)pswdIcon {
    if (!_pswdIcon) {
        _pswdIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_pswd"]];
    }
    return _pswdIcon;
}

- (UITextField *)accountTextField {
    if (!_accountTextField) {
        UITextField *field = [[UITextField alloc] init];
        field.borderStyle = UITextBorderStyleNone;
        [field setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        _accountTextField = field;
    }
    return _accountTextField;
}

- (UITextField *)pswdTextField {
    if (!_pswdTextField) {
        UITextField *field = [[UITextField alloc] init];
        field.borderStyle = UITextBorderStyleNone;
        field.secureTextEntry = YES;
        field.placeholder = @"请输入密码...";
        _pswdTextField = field;
    }
    return _pswdTextField;
}

- (UILabel *)messageLabel {
    if (!_messageLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = RGB16(0x999999);
        label.font = [UIFont systemFontOfSize:12];
        label.text = [NSString stringWithFormat:@"舟山远洋渔业小镇监控平台 V%@",[self currentBundleVersion]];
        _messageLabel = label;
    }
    return _messageLabel;
}

#pragma mark - Helper

- (NSString *)currentBundleVersion {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    return app_Version;
}

@end
